/*******************************************************
> Copyright (C) 2024 ==IOT== All rights reserved.
> File Name: enroll.c
> Author: sy
> Website:www.sy.org
> Created Time: 2024年09月20日 星期五 15时53分33秒
***************************************************/
// 管理员登录注册

#include "enroll.h"
char *sendto_and_receive(const char *buf, int sockfd)
{
    char dest[1024] = {0};
    // 发送
    if (send(sockfd, buf, strlen(buf), 0) == -1)
    {
        perror("send error");
        return NULL;
    }

    // 接收
    int ret = recv(sockfd, dest, sizeof(dest) - 1, 0);
    if (ret <= 0)
    {
        perror("recv error");
        return NULL;
    }

    dest[ret] = '\0';
    return strdup(dest);
}

void Register()
{
    struct manager man = {0}; // 初始化结构体
    printf("input your id, name, password\n");

    if (scanf("%9s %9s %9s", man.id, man.name, man.password) != 3)
    {
        fprintf(stderr, "输入格式错误\n");
        return;
    }

    printf("input your number\n");
    if (scanf("%lld", &man.number) != 1 || man.number < 10000000000LL || man.number > 99999999999LL)
    {
        fprintf(stderr, "请输入有效的手机号码（11位）\n");
        return;
    }

    printf("Please confirm your password\n");
    if (scanf("%9s", man.new_password) != 1)
    {
        fprintf(stderr, "密码输入错误\n");
        return;
    }

    if (strcmp(man.password, man.new_password) != 0)
    {
        fprintf(stderr, "两次密码不一致，请重新输入\n");
        return;
    }

    strcpy(man.type, "MANAGER");
    strcpy(man.directives, "Register");

    char *jsonStr = input_cJSON_reg(man);
    if (jsonStr == NULL)
    {
        fprintf(stderr, "生成JSON失败\n");
        return;
    }

    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1)
    {
        perror("socket");
        free(jsonStr);
        return;
    }

    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = inet_addr(IP);

    if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
    {
        perror("connect");
        close(sockfd);
        free(jsonStr);
        return;
    }

    char *response = sendto_and_receive(jsonStr, sockfd);
    free(jsonStr);
    close(sockfd);

    if (response)
    {
        output_cJSON_reg(response);
        free(response);
    }
    else
    {
        printf("未收到响应\n");
    }
}

char *input_cJSON_reg(struct manager man)
{
    cJSON *json = cJSON_CreateObject();
    if (!json)
        return NULL;

    cJSON_AddItemToObject(json, "type", cJSON_CreateString(man.type));
    cJSON_AddItemToObject(json, "directives", cJSON_CreateString(man.directives));
    cJSON_AddItemToObject(json, "id", cJSON_CreateString(man.id));
    cJSON_AddItemToObject(json, "name", cJSON_CreateString(man.name));
    cJSON_AddItemToObject(json, "password", cJSON_CreateString(man.password));
    cJSON_AddItemToObject(json, "number", cJSON_CreateNumber(man.number));

    char *dest = cJSON_Print(json);
    cJSON_Delete(json);
    return dest;
}

void output_cJSON_reg(const char *dest)
{
    cJSON *json = cJSON_Parse(dest);
    if (!json)
    {
        fprintf(stderr, "Failed to parse JSON response\n");
        return;
    }

    cJSON *node = cJSON_GetObjectItem(json, "directives");
    if (node && node->type == cJSON_String)
    {
        if (strcmp(node->valuestring, "Register") == 0)
        {
            node = cJSON_GetObjectItem(json, "ause");
            if (node && node->type == cJSON_String)
            {
                if (strcmp(node->valuestring, "success") == 0)
                {
                    printf("注册成功\n");
                }
                else
                {
                    printf("注册失败\n");
                    node = cJSON_GetObjectItem(json, "Info");
                    if (node && node->type == cJSON_String)
                    {
                        printf("info: %s\n", node->valuestring);
                    }
                }
            }
        }
    }

    cJSON_Delete(json);
}
